package com.rainng.coursesystem.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rainng.coursesystem.base.api.dto.PagingReqDTO;
import com.rainng.coursesystem.controller.BaseController;
import com.rainng.coursesystem.controller.admin.vo.VisBatchEntityVo;
import com.rainng.coursesystem.model.entity.VisBatchEntity;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import com.rainng.coursesystem.module.task.dto.request.TaskQueryReqDTO;
import com.rainng.coursesystem.module.task.dto.response.TaskPagingResDTO;
import com.rainng.coursesystem.module.task.service.BatchService;
import com.rainng.coursesystem.module.task.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Api(tags = "批次接口")
@RequestMapping("/admin/v2/batch")
@RestController
@Slf4j
public class BatchController extends BaseController {

    @Resource
    private BatchService batchService;




    //新增
    @PostMapping("/save")
    @ApiOperation(value = "新增批次,编辑批次")
    public ResultVO saveBatches(@RequestBody VisBatchEntity reqDTO) {
        if(reqDTO.getStartTime()== null || reqDTO.getEndTime() == null){
            return super.failedResult("批次开始或结束时间均不能为空");
        }
        if(reqDTO.getStartTime().after(reqDTO.getEndTime())){
            return super.failedResult("开始时间应早于结束时间");
        }

        if(reqDTO.getId() == null){
            log.info("此次为新增");
            reqDTO.setCreateTime(new Date());
            return super.result(batchService.save(reqDTO));
        }
        log.info("此次为修改");
        return super.result(batchService.updateById(reqDTO));
    }

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除批次")
    public ResultVO deleteBatches(@PathVariable String id){
        return super.result(batchService.removeById(id));
    }

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "删除批次")
    public ResultVO getDetailBatches(@PathVariable String id){
        return super.result(batchService.getById(id));
    }

    //tire/page
    @PostMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultVO pageQuery(@RequestBody VisBatchEntityVo reqDTO){
        List<VisBatchEntity> lists = batchService.getBatchList(reqDTO);
        return super.result(lists);
    }
    @PostMapping("/page/count")
    @ApiOperation(value = "分页查询数量")
    public ResultVO pageQueryCount(@RequestBody VisBatchEntityVo reqDTO){
        Integer i = batchService.getBatchCount(reqDTO);
        return super.result(i);
    }

    @GetMapping("/test")
    @ApiOperation(value = "测试接口")
    public ResultVO testBatch(){
        return super.result(batchService.getThisBatchName());
    }


}
